Imports System.Net
Imports System.Net.Sockets

Public Class MainListener

    Public Sub ServerProc()
        Try
            Dim port As Integer = Config.LoginServerPort
            Dim listener As TcpListener
            listener = New TcpListener(New System.Net.IPEndPoint(IPAddress.Any, port))
            listener.Start()
            gLog.LogMsg("Login server listening at port {0}", port)
            Do
                While listener.Pending() = False
                    Try
                        If gEvtStopServer.WaitOne(100, True) = True Then
                            gLog.LogDebug("Stopping server...")
                            Exit Sub
                        End If
                    Catch ex As Exception
                        gLog.LogError(ex)
                    End Try
                End While
                Dim client As NetworkHandler
                client = New NetworkHandler
                client.SessionManager = gSessionManager
                client.Socket = listener.AcceptSocket()
                Dim ipPoint As System.Net.IPEndPoint = CType(client.Socket.RemoteEndPoint, System.Net.IPEndPoint)
                gLog.LogMsg("Client connection accepted from {0}", ipPoint.Address.ToString())

                Dim thclient As Threading.Thread = New Threading.Thread(AddressOf client.Handle)
                gLog.LogDebug("creating client handling thread...")
                thclient.Start()
            Loop
        Catch ex As Exception
            gLog.LogError(ex)
        End Try
    End Sub
End Class
